package views.screens.dayScreens;

import controller.human.Action;
import controller.human.Person;
import controller.human.User;
import controller.util.Pair;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import views.MafiaFrame;
import views.panes.ButtonUser;
import views.panes.ButtonUserDead;
import views.panes.ButtonUserNoSound;
import views.panes.ButtonUserNoVote;
import views.panes.ButtonUserPropus;
import views.screens.Screen;

public final class DayScreen extends JPanel implements Screen{

    JScrollPane proposals = new JScrollPane();
    User killed;
    User handicapped;
    Action action;
    MafiaFrame mf;
    final ArrayList<Pair<String,String>> allProposals = new ArrayList<Pair<String, String>>();
    JPanel nightActions, players;

    public DayScreen(User killed, User handicapped, Action action, MafiaFrame mf) {
        this.killed = killed;
        this.handicapped = handicapped;
        this.action = action;
        this.mf = mf;
        paint();
    }

    void paintNightActions(){
        nightActions = new JPanel(new GridLayout(2,2));
        if(killed!=null){
            add(new JLabel("Died:"));
            add(new ButtonUserDead(killed.getName()));
        }
        if(handicapped!=null){
            add(new JLabel("Handicapped:"));
            if(action==Action.Mute)
                add(new ButtonUserNoSound(handicapped.getName()));
            else
                add(new ButtonUserNoVote(handicapped.getName()));
        }
    }

    public static int getNrCols(int nrPlayers) {
        if (nrPlayers < 9) {
            return 3;
        }
        if (nrPlayers < 16) {
            return 4;
        }
        return 5;
    }

    public static int getNrRows(int nrPlayers) {
        int nrCols = getNrCols(nrPlayers);
        return nrPlayers / nrCols;
    }
    
    void paintPlayers(){
        int nrCol = getNrCols(mf.players.size());
        int nrRows = getNrRows(mf.players.size());
        players = new JPanel(new GridLayout(nrRows, nrCol));
        Iterator<Person> it = mf.players.iterator();
        while (it.hasNext()) {
            Person p = it.next();
            JPanel button = null;
            switch (p.getPhysicalState()) {
                case Dead:
                    button = new ButtonUserDead(p.getName());
                    break;
                default:
                    if (p.getSocialState()<0) {
                        button = new ButtonUser(p.getName(), this);
                    } else {
                        button = new ButtonUserPropus(p.getName());
                    }
            }
            add(button);
        }
    }
    
    void paintProposals(){
        proposals = new JScrollPane();
        JPanel panel = new JPanel(new GridLayout(allProposals.size(), 3));
        proposals.add(panel);
        for(final Pair<String, String> proposal: allProposals){
            panel.add(new ButtonUser(proposal.getKey(), null));
            panel.add(new ButtonUserPropus(proposal.getValue()));
            JPanel jp = new JPanel();
            JButton cancel = new JButton("X");
            jp.add(cancel);
            cancel.addMouseListener(new MouseListener() {

                int id = allProposals.indexOf(proposal);

                @Override
                public void mouseClicked(MouseEvent e) {
                    allProposals.remove(id);
                    paint();
                    repaint();
                    mf.repaint();
                }

                @Override
                public void mousePressed(MouseEvent e) {
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                }

                @Override
                public void mouseExited(MouseEvent e) {
                }
            });
            panel.add(jp);
        }
    }

    public void paint(){
        setLayout(new BorderLayout());
        removeAll();
        paintNightActions();
        paintPlayers();
        paintProposals();

        add(nightActions, BorderLayout.NORTH);
        add(proposals, BorderLayout.EAST);
        add(players, BorderLayout.CENTER);
        JPanel sout = new JPanel();
        JButton next = new JButton("Next");
        sout.add(next);
        add(sout, BorderLayout.PAGE_END);
        next.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                ArrayList<Person> proposed = new ArrayList<Person>();
                for(Pair<String, String> proposal: allProposals){
                    Person p = mf.findUser(proposal.getValue());
                    if(!proposed.contains(p))
                        proposed.add(p);
                }
                System.out.println(proposed);
                //mf.update(new VotingScreen(mf, proposed));
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }

    @Override
    public void setAction(String action) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
